import overrideFn from "./override_or_new";
let Decimal = require('../miniprogram_npm/decimal.js/index');

function createImageNodeObjectFn (options) {
  let defaultObjectAttrs = {
    id: null, // 图片ID，为数据处理等用
    imageUrl: null, // 图片路径
    operableType: "empty", // 可操作类型
    width: null, // 当前宽
    height: null, // 当前高
    orientation: "up", // ? 拍摄方向 / 选择方向
    originalWidth: null, // 原来的宽
    originalHeight: null, // 原来的高
    top: 0, // 初始图片位置
    left: 0,
    x: 0, // 初始化圆心值x
    y: 0, // 初始化圆心值y
    lastX: 0, // 移动上一个点 X
    lastY: 0, // 移动上一个点 Y
    // firstX: 0, // 第一根手指 X
    // firstY: 0, // 第一根手指 Y
    // secondX: 0, // 第二根手指 X
    // secondY: 0, // 第二根手指 Y
    scale: 1, // 缩放比例, 1为不缩放
    previousScale: 1,
    scaleDistance: 0, // 
    angle: 0, // 旋转角度
    rotate: 0, // 旋转
  }

  let mergeKeys = ["imageUrl", "top", "left", "scale"];

  let imageNodeObject = overrideFn.objectMerge(defaultObjectAttrs, options, mergeKeys);
  if (imageNodeObject.originalWidth == null || imageNodeObject.originalHeight == null) {
    wx.getImageInfo({ 
      src: imageNodeObject.imageUrl,
      success: function (imageInfo) {
        imageNodeObject.originalWidth = imageInfo.width;
        imageNodeObject.originalHeight = imageInfo.height;
        imageNodeObject.orientation = imageInfo.orientation;
        imageNodeObject.x = Math.round(imageInfo.width / 2);
        imageNodeObject.y = Math.round(imageInfo.height / 2);
      },
      fail: function (err) {
        console.error("[获取图片信息失败]", err);
      }
    });
  }
  return imageNodeObject;
}

function dragStartFn (that, key, e) {
  let imageNode = that.data[key];
  if (imageNode["operableType"] == "empty") { 
    imageNode.lastX = e.touches[0].clientX;  // 记录点击时的坐标值  
    imageNode.lastY = e.touches[0].clientY;
    imageNode["operableType"] = "drag";
    return imageNode;
  } else {
    return imageNode;
  }
}

function dragMoveFn (that, key, e) {
  let imageNode = that.data[key];
  if (imageNode["operableType"] != "drag") { return imageNode; }
  let moveX = e.touches[0].clientX,  // 记录点击时的坐标值  
      moveY = e.touches[0].clientY;

  //追加改动值  
  imageNode.left += moveX - imageNode.lastX;  // x方向  
  imageNode.top += moveY - imageNode.lastY;    // y方向  
  imageNode.x += imageNode.lastX - imageNode.moveX;
  imageNode.y += imageNode.lastY - imageNode.moveY;

  imageNode.lastX = moveX;
  imageNode.lastY = moveY;

  return imageNode;
}

function scaleStartFn (that, key, e) {
  let imageNode = that.data[key];
  if (imageNode["operableType"] == 'empty' || imageNode["operableType"] == "drag") { 
    let point1 = e.touches[0];
    let point2 = e.touches[1];
    let xLen = Math.abs(Decimal.sub(point2.pageX, point1.pageX));
    let yLen = Math.abs(Decimal.sub(point2.pageY, point1.pageY));

    imageNode["scaleDistance"] = _getDistance(xLen, yLen);
    imageNode["operableType"] = "scale";
    imageNode["previousScale"] = 1;
    return imageNode;
  } else {
    return imageNode;
  }
}

function scaleMoveFn (that, key, e) {
  let imageNode = that.data[key];
  if (imageNode["operableType"] == "drag") { } else if (imageNode["operableType"] != "scale") { return imageNode; }

  let point1 = e.touches[0];
  let point2 = e.touches[1];
  let xLen = Math.abs(Decimal.sub(point2.pageX, point1.pageX));
  let yLen = Math.abs(Decimal.sub(point2.pageY, point1.pageY));

  let scaleDistance = _getDistance(xLen, yLen);
  if(imageNode["scaleDistance"]) {
    let scale = Decimal.div(scaleDistance, imageNode["scaleDistance"]);
    imageNode.scale = Decimal.sub(scale, imageNode["previousScale"]).add(imageNode["scale"]).toNumber();
    imageNode.previousScale = scale.toNumber();
  }

  return imageNode;
}

function _getDistance (xLen, yLen) {
  let xTwoPow = Decimal.mul(xLen, xLen),
      yTwoPow = Decimal.mul(yLen, yLen);
  return Math.sqrt(Decimal.add(xTwoPow, yTwoPow).toNumber());
}

/**
 * 通过改变 width 和 height 进行缩放
 */
// function scaleMoveFn (that, key, e) {
//   let imageNode = that.data[key];
//   let mFirstX = e.touches[0].clientX,
//       mFirstY = e.touches[0].clientY,
//       mSecondX = e.touches[1].clientX,
//       mSecondY = e.touches[1].clientY;

//   let xPow = Decimal.sub(imageNode.firstX, imageNode.secondX).toNumber(),
//       yPow = Decimal.sub(imageNode.firstY, imageNode.secondY).toNumber(),
//       mXPow = Decimal.sub(mFirstX, mSecondX).toNumber(),
//       mYPow = Decimal.sub(mFirstY, mSecondY).toNumber();
//   let initLen = Math.sqrt(Math.pow(xPow, 2) + Math.pow(yPow, 2)),
//       moveLen = Math.sqrt(Math.pow(mXPow, 2) + Math.pow(mYPow, 2));

//   let scale = Decimal.div(moveLen, initLen).toNumber();
//   let scale_width = Decimal.mul(imageNode.width, scale).toNumber(),
//       scale_height = Decimal.mul(imageNode.height, scale).toNumber(),
//       moveTop = Decimal.sub(imageNode.height, scale_height).div(2).toNumber(),
//       moveLeft = Decimal.sub(imageNode.width, scale_width).div(2).toNumber();

//       imageNode.width = scale_width;
//       imageNode.height = scale_height;

//       imageNode.firstX = mFirstX;
//       imageNode.firstY = mFirstY;
//       imageNode.secondX = mSecondX;
//       imageNode.secondY = mSecondY;

//       imageNode.top += moveTop;
//       imageNode.left += moveLeft;

//   return imageNode;
// }

function touchEnd (that, key, _e) {
  let imageNode = that.data[key];
  imageNode["operableType"] = 'empty';
  return imageNode;
}

module.exports = {
  createImageNodeObjectFn,
  dragStartFn,
  dragMoveFn,
  scaleStartFn,
  scaleMoveFn,
  touchEnd
}